import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/Home.vue'),
      meta: { title: '首页' }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/auth/Login.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/auth/Register.vue'),
      meta: { title: '注册' }
    },
    {
      path: '/products',
      name: 'products',
      component: () => import('@/views/product/ProductList.vue'),
      meta: { title: '商品列表' }
    },
    {
      path: '/products/:id',
      name: 'product-detail',
      component: () => import('@/views/product/ProductDetail.vue'),
      meta: { title: '商品详情' }
    },
    {
      path: '/brands',
      name: 'brands',
      component: () => import('@/views/Brands.vue'),
      meta: { title: '品牌馆' }
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('@/views/cart/Cart.vue'),
      meta: { title: '购物车', requiresAuth: true }
    },
    {
      path: '/orders',
      name: 'orders',
      component: () => import('@/views/order/OrderList.vue'),
      meta: { title: '我的订单', requiresAuth: true }
    },
    {
      path: '/user/orders',
      name: 'user-orders',
      component: () => import('@/views/order/OrderList.vue'),
      meta: { title: '我的订单', requiresAuth: true }
    },
    {
      path: '/orders/:id',
      name: 'order-detail',
      component: () => import('@/views/order/OrderDetail.vue'),
      meta: { title: '订单详情', requiresAuth: true }
    },
    {
      path: '/profile',
      name: 'profile',
      component: () => import('@/views/user/Profile.vue'),
      meta: { title: '个人中心', requiresAuth: true }
    },
    {
      path: '/checkout',
      name: 'checkout',
      component: () => import('@/views/checkout/Checkout.vue'),
      meta: { title: '结算订单', requiresAuth: true }
    },

    {
      path: '/notifications',
      name: 'notifications',
      component: () => import('@/views/user/Notifications.vue'),
      meta: { title: '我的通知', requiresAuth: true }
    },
    // 管理员页面
    {
      path: '/admin',
      name: 'admin',
      component: () => import('@/views/admin/Dashboard.vue'),
      meta: { title: '管理后台', requiresAuth: true, requiresAdmin: true },
      redirect: '/admin/analytics', // 默认重定向到数据分析页面
      children: [
        {
          path: 'users',
          name: 'admin-users',
          component: () => import('@/views/admin/UserManagement.vue'),
          meta: { title: '用户管理', requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'products',
          name: 'admin-products',
          component: () => import('@/views/admin/ProductManagement.vue'),
          meta: { title: '商品管理', requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'orders',
          name: 'admin-orders',
          component: () => import('@/views/admin/OrderManagement.vue'),
          meta: { title: '订单管理', requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'brands',
          name: 'admin-brands',
          component: () => import('@/views/admin/BrandManagement.vue'),
          meta: { title: '品牌管理', requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'categories',
          name: 'admin-categories',
          component: () => import('@/views/admin/CategoryManagement.vue'),
          meta: { title: '分类管理', requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'analytics',
          name: 'admin-analytics',
          component: () => import('@/views/admin/Analytics.vue'),
          meta: { title: '数据分析', requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'notifications',
          name: 'admin-notifications',
          component: () => import('@/views/admin/NotificationManagement.vue'),
          meta: { title: '通知管理', requiresAuth: true, requiresAdmin: true }
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  document.title = `${to.meta.title as string} - 商城系统`
  
  // 检查是否需要登录
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next({ name: 'login', query: { redirect: to.fullPath } })
    return
  }
  
  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin && !userStore.isAdmin) {
    next({ name: 'home' })
    return
  }
  
  // 如果已登录访问登录页，根据用户角色重定向
  if (to.name === 'login' && userStore.isLoggedIn) {
    if (userStore.isAdmin) {
      next({ name: 'admin' })  // 管理员跳转到管理后台
    } else {
      next({ name: 'home' })   // 普通用户跳转到首页
    }
    return
  }
  
  next()
})

export default router
